<?php
class XML
{
    /**
     *   将数组转换为xml
     *    @param array $data    要转换的数组
     *   @param bool $root     是否要根节点
     *   @return string         xml字符串
     *    @author Dragondean
     *    @url    http://www.cnblogs.com/dragondean
     */
    static function arr2xml($data, $root = true)
    {
        $str = "";
        if ($root) {
            $str .= "<xml>";
        }
        foreach ($data as $key => $val) {
            if (is_array($val)) {
                $child = XML::arr2xml($val, false);
                $str .= "<$key>$child</$key>";
            } else {
                $str .= "<$key><![CDATA[$val]]></$key>";
            }
        }
        if ($root) {
            $str .= "</xml>";
        }
        return $str;
    }

    static function xml2arr($xmlStr)
    {
        $xml_resp = simplexml_load_string(
            $xmlStr,
            'SimpleXMLElement',
            LIBXML_NOCDATA
        );
        return json_decode(json_encode($xml_resp), true);
    }
}
